package com.vrv.im.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vrv.im.domain.AnnexRelatedBean;
import com.vrv.im.domain.ComplaintBean;
import com.vrv.im.domain.ProjectBean;
import com.vrv.im.domain.UserBean;
import com.vrv.im.persistence.AnnexMapper;
import com.vrv.im.persistence.AnnexRelatedMapper;
import com.vrv.im.persistence.ComplaintMapper;
import com.vrv.im.persistence.ProjectMapper;
import com.vrv.im.persistence.UserMapper;
import com.vrv.im.service.ComplaintService;
import com.vrv.im.util.Constant;
import com.vrv.im.vo.ComplaintVo;

@Service
public class ComplaintServiceImpl implements ComplaintService {

	@Autowired
	private ComplaintMapper complaintMapper;
	@Autowired
	private ProjectMapper projectMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private AnnexRelatedMapper annexReMapper;
	@Autowired
	private AnnexMapper annexMapper;

	public long addComplaint(ComplaintBean b) {
		long complaintID = complaintMapper.addComplain(b);
		return complaintID;
	}

	@Override
	public List<ComplaintVo> getAllComplaint() {
		List<ComplaintVo> result = new ArrayList<ComplaintVo>();
		List<ComplaintBean> list = complaintMapper.getAllComplaint();
		if (list != null && list.size() > 0) {
			for (ComplaintBean b : list) {
				ComplaintVo v = new ComplaintVo();
				v.setProjectID(b.getProjectID());
				ProjectBean projectBean = projectMapper.getProjectByID(b.getProjectID());
				if (projectBean != null) {
					v.setCommunityName(projectBean.getCommunityName());
					v.setCommunityNum(projectBean.getCommunityNum());
					v.setContractNum(projectBean.getContractNum());
				}
				v.setUserID(b.getUserID());
				UserBean ub = userMapper.getUserByUserID(b.getUserID());
				if (ub != null) {
					v.setUserName(ub.getName());
					v.setPhone(ub.getPhone());
				}
				v.setProblem(b.getProblem());
				v.setCreateTime(new Date(b.getComplaintTime()));
				AnnexRelatedBean annexRelatedBean = new AnnexRelatedBean();
				annexRelatedBean.setTypeID(Constant.ANNEX_TYPE_COMPLAINT);
				annexRelatedBean.setLeadID(b.getComplaintID());
				List<Long> fileIDs = annexReMapper.getFileID(annexRelatedBean);
				if (fileIDs != null && fileIDs.size() > 0) {
					Map<String, List<Long>> map = new HashMap<String, List<Long>>();
					map.put("list", fileIDs);
					List<String> fileUrls = annexMapper.getAnnex(map);
					if (fileUrls != null && fileUrls.size() > 0)
						v.setFileUrls(fileUrls);
				}
				result.add(v);
			}
		}
		return result;
	}

}
